<?php

namespace app\mobile\controller;

use think\Controller;

class Common extends Controller {

        public function _initialize() {
                $_REQUEST = array_merge($_GET, $_POST);
                //当前访问域名过滤
                $murl = substr($_SERVER['SERVER_NAME'], 0, 1);
                if ($murl != 'm') {
                        $this->_empty();
                }


                //获取当前访问域名
                session('current_domain', '@m');
                $this->assign('current_domain', '@m');
                $this->assign('domain_url', 'http://' . '@m' . '.' . C('site.sitedomain'));
                //页脚
                $footlink = array();
                $about = M("Aboutus")->cache()->field('id,title')->select();
                foreach ($about as $key => $val) {
                        $footlink[] = $val;
                        $footlink[$key]['url'] = U('/aboutus/' . $val['id'] . '@m');
                }
                $this->assign('footlink', $footlink);
        }

        public function _empty() {
                C('HTML_CACHE_ON', false);
                send_http_status(404);
                $tours = D("Tours")->cache()->order("rand()")->limit(5)->select();
                $this->assign('tours', $tours);
                return $this->fetch("public:_404");
        }

        /**
         * Ajax方式返回数据到客户端
         * @access protected
         * @param mixed $data 要返回的数据
         * @param String $type AJAX返回数据格式
         * @return void
         */
        protected function ajaxReturn($data, $type = '') {
                if (func_num_args() > 2) {// 兼容3.0之前用法
                        $args = func_get_args();
                        array_shift($args);
                        $info = array();
                        $info['data'] = $data;
                        $info['info'] = array_shift($args);
                        $info['status'] = array_shift($args);
                        $data = $info;
                        $type = $args ? array_shift($args) : '';
                }
                if (empty($type))
                        $type = C('DEFAULT_AJAX_RETURN');
                switch (strtoupper($type)) {
                        case 'JSON' :
                                // 返回JSON数据格式到客户端 包含状态信息
                                header('Content-Type:application/json; charset=utf-8');
                                exit(json_encode($data));
                        case 'XML' :
                                // 返回xml格式数据
                                header('Content-Type:text/xml; charset=utf-8');
                                exit(xml_encode($data));
                        case 'JSONP':
                                // 返回JSON数据格式到客户端 包含状态信息
                                header('Content-Type:application/json; charset=utf-8');
                                $handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
                                exit($handler . '(' . json_encode($data) . ');');
                        case 'EVAL' :
                                // 返回可执行的js脚本
                                header('Content-Type:text/html; charset=utf-8');
                                exit($data);
                        default :
                                // 用于扩展其他返回格式数据
                                tag('ajax_return', $data);
                }
        }

        

}
